import Vue from 'vue'
import Router from 'vue-router'
import store from '@/vuex/store'

Vue.use(Router)

const Layout = resolve => require(['@/templates/layout.vue'], resolve)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: resolve => require(['@/views/login.vue'], resolve)
    },
    {
      path: '/index',
      component: Layout,
      children: [
        {
          path: '',
          meta: {
            title: '首页'
          },
          component: resolve => require(['@/views/index.vue'], resolve)
        },

      ]
    },
    {
      path: '/message',
      component: Layout,
      children: [
        {
          path: '',
          meta: {
            title: '消息'
          },
          component: resolve => require(['@/views/message/index.vue'], resolve)
        },

      ]
    },
    {
      path: '/workbench',
      component: Layout,
      children: [
        {
          path: 'case',
          name: 'case',
          meta: {
            title: '我的案件'
          },
          component: resolve => require(['@/views/workbench/case.vue'], resolve)
        },
        {
          path: 'case/pause',
          name: 'casePause',
          meta: {
            title: '我的暂停案件'
          },
          component: resolve => require(['@/views/workbench/casePause.vue'], resolve)
        },
        {
          path: 'case/close',
          name: 'caseClose',
          meta: {
            title: '我的结案案件'
          },
          component: resolve => require(['@/views/workbench/caseClose.vue'], resolve)
        },
        {
          path: 'case/change',
          name: 'caseChange',
          meta: {
            title: '我的转案案件'
          },
          component: resolve => require(['@/views/workbench/caseChange.vue'], resolve)
        }
      ]
    },
    {
      path: '/clue',
      component: Layout,
      children: [
        {
          path: 'add',
          name: 'addClue',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/clue/add.vue'], resolve)
        },
        {
          path: 'detail',
          name: 'detailClue',
          meta: {
            title: '线索详情'
          },
          component: resolve => require(['@/views/clue/detail.vue'], resolve)
        },
        {
          path: 'list',
          name: 'clue',
          meta: {
            title: '线索管理'
          },
          component: resolve => require(['@/views/clue/index.vue'], resolve)
        },
        {
          path: 'invalid',
          name: 'invalidClue',
          meta: {
            title: '已作废线索'
          },
          component: resolve => require(['@/views/clue/invalid.vue'], resolve)
        },
        // {
        //   path: 'import',
        //   name: 'importClue',
        //   meta: {
        //     title: '导入线索',
        //     permissions: []
        //   },
        //   component: resolve => require(['@/views/clue/import.vue'], resolve)
        // },
      ]
    },
    {
      path: '/case',
      component: Layout,
      children: [
        {
          path: 'add',
          name: 'addCase',
          meta: {
            title: '添加案件'
          },
          component: resolve => require(['@/views/case/add.vue'], resolve)
        },
        {
          path: 'detail',
          name: 'detailCase',
          meta: {
            title: '案件详情'
          },
          component: resolve => require(['@/views/case/detail.vue'], resolve)
        },
        {
          path: 'list',
          name: 'case',
          meta: {
            title: '案件管理'

          },
          component: resolve => require(['@/views/case/index.vue'], resolve)
        },
        {
          path: 'closed',
          name: 'closedCase',
          meta: {
            title: '已结案件'
          },
          component: resolve => require(['@/views/case/closed.vue'], resolve)
        }
      ]
    },
    {
      path: '/approval',
      component: Layout,
      children: [
        {
          path: 'proposal',
          name: 'proposal',
          meta: {
            title: '线索提案审批'

          },
          component: resolve => require(['@/views/approval/proposal.vue'], resolve)
        },
        {
          path: 'pause',
          name: 'pause',
          meta: {
            title: '暂停案件审批'

          },
          component: resolve => require(['@/views/approval/pause.vue'], resolve)
        },
        {
          path: 'change',
          name: 'change',
          meta: {
            title: '转案案件审批'

          },
          component: resolve => require(['@/views/approval/change.vue'], resolve)
        },
        {
          path: 'close',
          name: 'close',
          meta: {
            title: '结案案件审批'

          },
          component: resolve => require(['@/views/approval/close.vue'], resolve)
        }
      ]
    },
    {
      path: '/tools',
      component: Layout,
      children: [
        {
          path: '',
          name: 'index',
          meta: {
            title: '达飞数据查询'

          },
          component: resolve => require(['@/views/tools/index.vue'], resolve)
        }
      ]
    },
    {
      path: '/system',
      component: Layout,
      children: [
        {
          path: 'user',
          name: 'user',
          meta: {
            title: '用户管理'

          },
          component: resolve => require(['@/views/system/user.vue'], resolve)
        },
        {
          path: 'user/edit',
          name: 'userEdit',
          meta: {
            title: '编辑用户'

          },
          component: resolve => require(['@/views/system/userEdit.vue'], resolve)
        },
        {
          path: 'user/detail',
          name: 'userDetail',
          meta: {
            title: '用户详情'
          },
          component: resolve => require(['@/views/system/userDetail.vue'], resolve)
        },
        {
          path: 'role',
          name: 'role',
          meta: {
            title: '角色管理'

          },
          component: resolve => require(['@/views/system/role.vue'], resolve)
        },
        {
          path: 'org',
          name: 'org',
          meta: {
            title: '架构管理'

          },
          component: resolve => require(['@/views/system/org.vue'], resolve)
        },
        {
          path: 'org/edit',
          name: 'orgEdit',
          meta: {
            title: '编辑架构'

          },
          component: resolve => require(['@/views/system/orgEdit.vue'], resolve)
        },
        {
          path: 'org/detail',
          name: 'orgDetail',
          meta: {
            title: '架构详情'
          },
          component: resolve => require(['@/views/system/orgDetail.vue'], resolve)
        }
      ]
    },
    {
      path: '/system',
      component: Layout,
      children: [
        {
          path: 'resource',
          name: 'resource',
          meta: {
            title: '资源管理'
          },
          component: resolve => require(['@/views/system/resource.vue'], resolve)
        }
      ]
    },
    {
      path: '/system',
      component: Layout,
      children: [
        {
          path: 'dept',
          name: 'dept',
          meta: {
            title: '营业部管理'
          },
          component: resolve => require(['@/views/system/dept.vue'], resolve)
        }
      ]
    },
    {
      path: '/*',
      component: Layout,
      children: [
        {
          path: '',
          meta: {
            title: '404-页面不存在'
          },
          component: resolve => require(['@/views/error/404.vue'], resolve)
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (store.getters.userInfo.token) {
    if (to.path == '/login') {
      next({ path: '/index' })
    }
    else {
      next()
    }
  }
  else {
    if (to.path !== '/login') {
      // next({ path: '/login', query: { redirect: to.fullPath } })
      next({ path: '/login' })
    }
    else {
      next()
    }
  }
})

router.afterEach((to, from) => {
  window.scrollTo(0, 0)
  if (to.path.indexOf('/system') > -1) {
    store.state.common.isSystem = true
  }
  else {
    store.state.common.isSystem = false
  }
})

export default router